package com.ysd.twocrm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysd.twocrm.entity.EmailUtils;
import com.ysd.twocrm.entity.Wage;
import com.ysd.twocrm.mapper.WageMapper;
import com.ysd.twocrm.service.MailService;
import com.ysd.twocrm.service.WageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/wage")
public class WageController {
    @Autowired
    private WageMapper wageMapper;
    @Autowired
    private WageService wageService;

    /*
     * @Description 首先去判断当前用户是否已经在本月发过工资，如果发过则return，没发过就调用发工资的方法
     * @param wage
     * @param name
     * @param userProtectMail
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/6/1 23:21
     */
    @PostMapping
    public Object findIfWageById(Wage wage,String name,String userProtectMail){


        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<Wage> queryWrapper=new QueryWrapper<Wage>();
        queryWrapper.eq("userId",wage.getUserId()).likeRight("submittime",sdf.format(new Date()));
        Wage ifwage = wageMapper.selectOne(queryWrapper);
        if(ifwage!=null){
            return "false";
        }else{
            try {
                Integer insertwage = wageService.insertwage(name,userProtectMail,wage.getUserId());
                if(insertwage==0){
                    return "insertfalse";
                }
            } catch (ParseException e) {
                return "insertfalse";

            }
            return "success";
        }

    }

    /*
     * @Description 上面判断了本月是否发送过工资后，如果用户确定了，则就要先调用这个方法来进行删除了
     * @param wage
     * @param name
     * @param userProtectMail
     * @return java.lang.Object
     * @author liusupeng
     * @date 2020/6/1 23:22
     */
    @DeleteMapping
    public Object deleteWage(Wage wage,String name,String userProtectMail){


        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<Wage> queryWrapper=new QueryWrapper<Wage>();
        queryWrapper.eq("userId",wage.getUserId()).likeRight("submittime",sdf.format(new Date()));
        Wage ifwage = wageMapper.selectOne(queryWrapper);
        int i = wageMapper.deleteById(ifwage.getWageId());
        if(i>0){
            findIfWageById(wage,name,userProtectMail);
            return "success";
        }
        return "false";
    }
}
